# filename: $exporterShortName.py
# "sh" export module, version 1.0 

import string
import os
import os.path
import sys
import log

class parseBuild():        
    global exportFile 
    exportFile = ""
    
    global commands
    commands = {}
    
    global resources
    resources = {}
 
    global VERBOSE
    VERBOSE = True
    
    global TIMERESOURCES
    TIMERESOURCES = True   
    
    def __init__(self, exportFile, commands, resources, VERBOSE, TIMERESOURCES):
        self.exportFile = exportFile
        self.commands = commands
        self.resources = resources        
        self.VERBOSE = VERBOSE
        self.TIMERESOURCES = TIMERESOURCES
                
        log.setLog("infoLog")        

        self.doExport(exportFile, commands, resources, VERBOSE, TIMERESOURCES)


    def doExport(self, exportFile, commands, resources, VERBOSE, TIMERESOURCES):
        
        if os.access(exportFile, os.W_OK):
            if os.path.exists(exportFile):
                if os.path.isfile(exportFile):
                    os.remove(exportFile)
                
        # open file for writing
        fileDescriptor = open(exportFile, 'w')
        
        fileDescriptor.write("#!/bin/sh\n")
        
        # some useful information
        
        fileDescriptor.write("# file: " + str(resources['pkgDescriptorFormal']['pkgName']) + "-" + str(resources['pkgDescriptorFormal']['pkgVersion']) + "-" + str(resources['pkgDescriptorFormal']['pkgRevision']) + ".sh\n\n")
        fileDescriptor.write("# pkg-name: " + str(resources['pkgDescriptorFormal']['pkgName']) + "\n")
        fileDescriptor.write("# pkg-version: " + str(resources['pkgDescriptorFormal']['pkgVersion']) + "\n")
        fileDescriptor.write("# pkg-revision: " + str(resources['pkgDescriptorFormal']['pkgRevision']) + "\n")
        fileDescriptor.write("# copyright: " + str(resources['pkgDescriptorInformation']['license']) + "\n")
        fileDescriptor.write("# summary: " + str(resources['pkgDescriptorInformation']['summary']) + "\n\n")

        # write the commands
        for command in commands:
            fileDescriptor.write(command + "\n")
            
        fileDescriptor.write("\n# generated by apkg 0.7 - heuristic [automake] 1.0\n")

        # close file
        fileDescriptor.close()
                